package com.yljk.mydoctor.activity;


import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.NetWorkUtils;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.LoadingUtils;
import com.yljk.mydoctor.R;
import com.yljk.servicemanager.constants.ClickApi;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.constants.UrlApi;
import com.yljk.servicemanager.oldbase.ModuleBaseActivity;
import com.yljk.servicemanager.bean.ProtocalEntity;
import com.yljk.servicemanager.interfaceapi.MineBackApi;
import com.yljk.servicemanager.interfaceapi.NetWorkCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import crossoverone.statuslib.StatusUtil;

/**
 * Created by SHICHENG
 * on 2021/10/22.
 */
@Route(path = RouterApi.SettingActivity)
public class SettingActivity extends ModuleBaseActivity {



    private TextView versionTv;
    private TextView logoutTv;
    private View inflate;
    private TextView cancelLogout;
    private TextView logout;
    private LinearLayout agreementLl;
    private LinearLayout privacyLl;

    private PopupWindow popupWindow;

    @Override
    public int bondLayout() {
        return R.layout.activity_setting;
    }

    @Override
    public void initView() {
        ClickUtils.sendEvent(ClickApi.Mine.Mine_Setting_Load,this,"设置页面载入");
        if (Build.VERSION.SDK_INT >= 21) {
            StatusUtil.setUseStatusBarColor(this, getResources().getColor(R.color.white));
            StatusUtil.setSystemStatus(this, false, false);
        }

        StatusUtil.setSystemStatus(this, false, true);
        versionTv = findViewById(R.id.tv_version);
        logoutTv = findViewById(R.id.tv_logout);

        inflate = getLayoutInflater().inflate(R.layout.logout_pop, null);
        cancelLogout = inflate.findViewById(R.id.cancel_logout);
        logout = inflate.findViewById(R.id.logout);
        popupWindow = new PopupWindow();
        popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        popupWindow.setContentView(inflate);

        agreementLl = findViewById(R.id.ll_agreement);
        privacyLl = findViewById(R.id.ll_privacy);
    }

    @Override
    public void initData() {
        versionTv.setText(DevicesUtils.getVersionName());

    }

    @Override
    public void initEvent() {
        ClickUtils.setFastOnClickListener(agreementLl, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.Mine_Zcprotocal,SettingActivity.this,"医生注册及服务协议点击");
                getRegistText("yszcxy");
            }
        });
        ClickUtils.setFastOnClickListener(privacyLl, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.Mine_XY,SettingActivity.this,"法律声明及隐私权政策点击");
                getRegistText("flsmjyszc");
            }
        });
        ClickUtils.setFastOnClickListener(findViewById(R.id.back_iv), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ClickUtils.setFastOnClickListener(logoutTv, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.Mine_Logout,SettingActivity.this,"退出登录点击出现弹窗");
                popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
                backgroundAlpha(0.5f);
//                setResult(LOGOUT_RESULT);
//                finish();

//                boolean b = SharedPreferencesUtils.getInstance().removeAll(SettingActivity.this);
//                if (b) {
//                    ARouter.getInstance().build(RouterApi.CenterActivity).navigation();
////                    Intent loginintent = new Intent(AllStringConstants.BroadcastReceiver);
////                    loginintent.putExtra(AllStringConstants.islogin, true);         //向广播接收器传递数据
////                    sendBroadcast(loginintent);      //发送广播
//
//                    finish();
//                }

            }
        });

        ClickUtils.setFastOnClickListener(cancelLogout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.Mine_CancelLogout,SettingActivity.this,"退出登录弹窗取消");
                popupWindow.dismiss();
            }
        });
        ClickUtils.setFastOnClickListener(logout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.Mine_LogoutSure,SettingActivity.this,"退出登录弹窗点击确定");
                popupWindow.dismiss();
                MineBackApi mineBackApi = (MineBackApi) ARouter.getInstance().build(RouterApi.MINEBACKIMPL).navigation();
                mineBackApi.onCancelLogout(SettingActivity.this, SettingActivity.this);
//                LoadingUtils.getLoadingUtils(SettingActivity.this).showLoading();
//                //退出服务器
//                NetWorkUtils.initNetWorkUtils(SettingActivity.this).post( DevicesUtils.getCurrentUrl() + AllStringConstants.mine_lagout, new JSONObject(), new NetWorkCallBack() {
//                    @Override
//                    public void onSuccess(String resultMsg, int code) {
////                        {"code":200,"message":null,"data":""}
////                        LogoutBean logoutBean = new Gson().fromJson(resultMsg, LogoutBean.class);
////                        if (logoutBean.isData()) {
////
////                        }
//
//                        LoadingUtils.getLoadingUtils(SettingActivity.this).dismissLoading();
////                        if (code == 200) {
////                            popupWindow.dismiss();
////                            setResult(LOGOUT_RESULT);
////                            finish();
////                        }
//
//                    }
//
//                    @Override
//                    public void onFailure(String failureMsg) {
//
//                        LoadingUtils.getLoadingUtils(SettingActivity.this).dismissLoading();
//                        if (!TextUtils.isEmpty(failureMsg)) {
//                            Toast.makeText(SettingActivity.this, failureMsg, Toast.LENGTH_SHORT).show();
//                        }
//                    }
//
//                    @Override
//                    public void onException(Exception exceptionMsg) {
//
//                        LoadingUtils.getLoadingUtils(SettingActivity.this).dismissLoading();
//                    }
//
//                });
//
//
////                boolean b = SharedPreferencesUtils.getInstance().removeAll(SettFingActivity.this);
////                if (b) {
////                    ARouter.getInstance().build(RouterApi.CenterActivity).navigation();
//////                    Intent loginintent = new Intent(AllStringConstants.BroadcastReceiver);
//////                    loginintent.putExtra(AllStringConstants.islogin, true);         //向广播接收器传递数据
//////                    sendBroadcast(loginintent);      //发送广播
////
////                    finish();
////                }

            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
            }
        });

    }

    private boolean isLock = false;

    /**
     * 获取注册协议文案
     */
    void getRegistText(String documentNo) {
        if (isLock) {
            return;
        }
        isLock = true;
        RequestParams params = new RequestParams();
        params.put("documentNo", documentNo);
        LoadingUtils.getLoadingUtils(this).showLoading();
        HttpUtils.get(UrlApi.getByDocumentNo, params, new HttpCallBack<ProtocalEntity>() {
            @Override
            public void onSuccess(ProtocalEntity protocalEntity, int code) {
                isLock = false;
                if (protocalEntity.isResult() && protocalEntity.getData() != null) {
                    ARouter.getInstance().build(RouterApi.WebActivity)
                            .withString("title", protocalEntity.getData().getTitle())
                            .withString("url", protocalEntity.getData().getContent())
                            .withBoolean("ishtml", true)
                            .navigation();
                }
            }

            @Override
            public void onFailure(String failureMsg, int statusCode) {
                isLock = false;
                LoadingUtils.getLoadingUtils(SettingActivity.this).dismissLoading();
            }
        });
    }

    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0~1.0
        getWindow().setAttributes(lp);
    }



}
